package org.karelun.rabbit.bridge.ui.action;

import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.part.EditorActionBarContributor;
import org.eclipse.ui.part.IPage;
import org.eclipse.ui.views.properties.IPropertySheetPage;
import org.karelun.rabbit.bridge.ITransactionEditingDomainProvider;

public class RabbitEditorActionBarContributor extends
		EditorActionBarContributor implements IMenuListener {

	protected IEditorPart activeEditor;

	protected RabbitCopyAction copyAction;

	protected RabbitCutAction cutAction;

	protected RabbitPasteAction pasteAction;

	protected RabbitDeleteAction deleteAction;

	protected RabbitUndoAction undoAction;

	protected RabbitRedoAction redoAction;

	public static final int ADDITIONS_LAST_STYLE = 0x1;

	protected int style;

	public RabbitEditorActionBarContributor() {
	}

	public RabbitEditorActionBarContributor(int style) {
		this.style = style;
	}

	@Override
	public void init(IActionBars bars) {
		super.init(bars);
		ISharedImages sharedImages = PlatformUI.getWorkbench()
				.getSharedImages();

		copyAction = new RabbitCopyAction();
		copyAction.setImageDescriptor(sharedImages
				.getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
		bars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyAction);

		cutAction = new RabbitCutAction();
		cutAction.setImageDescriptor(sharedImages
				.getImageDescriptor(ISharedImages.IMG_TOOL_CUT));
		bars.setGlobalActionHandler(ActionFactory.CUT.getId(), cutAction);

		pasteAction = new RabbitPasteAction();
		pasteAction.setImageDescriptor(sharedImages
				.getImageDescriptor(ISharedImages.IMG_TOOL_PASTE));
		bars.setGlobalActionHandler(ActionFactory.PASTE.getId(), pasteAction);

		deleteAction = new RabbitDeleteAction(removeAllReferencesOnDelete());
		deleteAction.setImageDescriptor(sharedImages
				.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
		bars.setGlobalActionHandler(ActionFactory.DELETE.getId(), deleteAction);

		undoAction = new RabbitUndoAction();
		undoAction.setImageDescriptor(sharedImages
				.getImageDescriptor(ISharedImages.IMG_TOOL_UNDO));
		bars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);

		redoAction = new RabbitRedoAction();
		redoAction.setImageDescriptor(sharedImages
				.getImageDescriptor(ISharedImages.IMG_TOOL_REDO));
		bars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction);
	}

	public void shareGlobalActions(IPage page, IActionBars actionBars) {
		if (!(page instanceof IPropertySheetPage)) {
			actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(),deleteAction);
			actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(),cutAction);
			actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(),copyAction);
			actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(),pasteAction);
		}
		actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(),undoAction);
		actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(),redoAction);
	}

	public IEditorPart getActiveEditor() {
		return activeEditor;
	}

	public void setActiveEditor(IEditorPart editorPart) {
		if (editorPart != activeEditor) 
		{
			if (activeEditor != null) {
				deactivate();
			}
			if (editorPart instanceof ITransactionEditingDomainProvider) {
				activeEditor = editorPart;
				activate();
			}
		}
	}

	private void activate() {
		copyAction.setActiveWorkbenchPart(activeEditor);
		cutAction.setActiveWorkbenchPart(activeEditor);
		pasteAction.setActiveWorkbenchPart(activeEditor);
		deleteAction.setActiveWorkbenchPart(activeEditor);
		undoAction.setActiveWorkbenchPart(activeEditor);
		redoAction.setActiveWorkbenchPart(activeEditor);

		ISelectionProvider selectionProvider = (activeEditor instanceof ISelectionProvider) ? (ISelectionProvider) activeEditor
				: activeEditor.getEditorSite().getSelectionProvider();
		if (selectionProvider != null) {
			selectionProvider.addSelectionChangedListener(copyAction);
			selectionProvider.addSelectionChangedListener(cutAction);
			selectionProvider.addSelectionChangedListener(pasteAction);
			selectionProvider.addSelectionChangedListener(deleteAction);
		}
		update();
	}

	private void deactivate() {
		copyAction.setActiveWorkbenchPart(null);
		cutAction.setActiveWorkbenchPart(null);
		pasteAction.setActiveWorkbenchPart(null);
		deleteAction.setActiveWorkbenchPart(null);
		undoAction.setActiveWorkbenchPart(null);
		redoAction.setActiveWorkbenchPart(null);

		ISelectionProvider selectionProvider = (activeEditor instanceof ISelectionProvider) ? (ISelectionProvider) activeEditor
				: activeEditor.getEditorSite().getSelectionProvider();
		if (selectionProvider != null) {
			selectionProvider.removeSelectionChangedListener(copyAction);
			selectionProvider.removeSelectionChangedListener(cutAction);
			selectionProvider.removeSelectionChangedListener(pasteAction);
			selectionProvider.removeSelectionChangedListener(deleteAction);
		}
	}

	public void update() {
		ISelectionProvider selectionProvider = (activeEditor instanceof ISelectionProvider) ? (ISelectionProvider) activeEditor
				: activeEditor.getEditorSite().getSelectionProvider();
		if (selectionProvider != null) {
			ISelection selection = selectionProvider.getSelection();
			IStructuredSelection ss = (selection instanceof IStructuredSelection) ? (IStructuredSelection) selection
					: StructuredSelection.EMPTY;

			copyAction.updateSelection(ss);
			cutAction.updateSelection(ss);
			pasteAction.updateSelection(ss);
			deleteAction.updateSelection(ss);
			undoAction.update();
			redoAction.update();
		}
	}

	@Override
	public void menuAboutToShow(IMenuManager manager) {
		if ((style & ADDITIONS_LAST_STYLE) == 0) {
			manager.add(new Separator("additions"));
		}
		manager.add(new Separator("edit"));

		manager.add(new ActionContributionItem(undoAction));
		manager.add(new ActionContributionItem(redoAction));
		manager.add(new Separator());
		manager.add(new ActionContributionItem(copyAction));
		manager.add(new ActionContributionItem(cutAction));
		manager.add(new ActionContributionItem(pasteAction));
		manager.add(new Separator());
		manager.add(new ActionContributionItem(deleteAction));

		if ((style & ADDITIONS_LAST_STYLE) != 0) {
			manager.add(new Separator("additions"));
			manager.add(new Separator());
		}
		manager.add(new Separator("additions-end"));
		addGlobalActions(manager);
	}

	protected void addGlobalActions(IMenuManager manager) {
		// String key=(style &
		// ADDITIONS_LAST_STYLE)==0?"additions-end":"additions";
	}

	protected boolean removeAllReferencesOnDelete() {
		return false;
	}

}
